//
//  GovWindowInfoViewController.m
//  homeland
//
//  Created by SunShine on 14-1-19.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "GovWindowInfoViewController.h"
#import "GovCenterService.h"
#import "GovWindwoDetailViewController.h"
#import "EnterpriseWorkEntity.h"

@interface GovWindowInfoViewController ()

@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *dataList;
@end

@implementation GovWindowInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    EnterpriseWorkEntity *entity = [self.config objectForKey:@"dataItem"];
    self.title = entity.businessName;
    [self loadDataForKey:entity.businessName];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Data Method
-(void)loadDataForKey:(NSString *)key
{
    [self startLoadingView];
    [[GovCenterService new] getWindowInfoForKey:key Success:^(NSArray *data) {
        self.dataList = data;
        [self.tableView reloadData];
        [self stopLoadingView];
    } Failure:^(NSString *msg) {
        [self showAlertWithTitle:@"错误" Msg:msg];
        [self stopLoadingView];
    }];
}

#pragma mark - Table Method
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return (self.dataList?self.dataList.count:0);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"GovWindowListCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    NSDictionary *dic = [self.dataList objectAtIndex:indexPath.row];
    cell.textLabel.text = [dic objectForKey:@"project"];
    cell.detailTextLabel.text = [dic objectForKey:@"name"];
    cell.detailTextLabel.textColor = [UIColor blackColor];
    cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:17.0];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    GovWindwoDetailViewController *detailVC = [[GovWindwoDetailViewController alloc] initWithNibName:@"GovWindwoDetailViewController" bundle:nil];
    detailVC.dataItem = [self.dataList objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
}
@end
